package com.gwtstructs.gwt.client.widgets.autocompleterTextbox;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class GraphicEnabledAutocompleterTextbox extends AutoCompleterTextBox {
	
	private FlowPanel textBoxPanel;
	private List<String> selectedStringList = new ArrayList<String>();

	public GraphicEnabledAutocompleterTextbox(List<String> suggestions, boolean isComparisonCaseSensitive
			, boolean isComparisonStartsFromBeginning, PanelPosition position) {
		this(new TextBox(), new FlowPanel(), suggestions, position);
	}
	public GraphicEnabledAutocompleterTextbox(List<String> suggestions, boolean isComparisonCaseSensitive
			, PanelPosition position) {
		this(suggestions, isComparisonCaseSensitive, false, position);
	}
	public GraphicEnabledAutocompleterTextbox(final List<String> suggestions, PanelPosition position) {
		this(suggestions, false, position);
	}
	public GraphicEnabledAutocompleterTextbox(final List<String> suggestions) {
		this(suggestions, PanelPosition.BOTTOM);
	}
	protected GraphicEnabledAutocompleterTextbox(TextBox textBox, final List<String> suggestions, PanelPosition position){
		this(textBox, textBox, suggestions, position);
	}
	protected GraphicEnabledAutocompleterTextbox(final TextBox textBox, Widget textboxContainer, final List<String> suggestions
			, PanelPosition position){
		super(textBox, textboxContainer, suggestions, position);
		textBoxPanel = (FlowPanel) super.getTextBoxContainer();
		textBoxPanel.add(textBox);
		textBoxPanel.setWidth("100%");
		textBoxPanel.setStyleName("autocomplete-graphic-textboxpanel");
		DOM.setStyleAttribute(textBox.getElement(), "border", "none 0px black");
		DOM.setStyleAttribute(textBox.getElement(), "outline", "none 0px black");
		DOM.setStyleAttribute(textBox.getElement(), "display", "block");
	}
	@Override
	public void onLoad() {
		super.getTextBox().setWidth(textBoxPanel.getElement().getClientWidth() + "px");
	}
	@Override
	public String getText() {
		if(selectedStringList.isEmpty())
			return "";
		else{
			StringBuffer buffer = new StringBuffer(selectedStringList.size() * 10);
			for (int i = 0; i < selectedStringList.size(); i++)
				buffer.append(selectedStringList.get(i)).append(",");
			return buffer.deleteCharAt(buffer.length() - 1).toString();
		}
	}
	@Override
	protected String getCurrentText() {
		return "";
	}
	@Override
	public String getValue() {
		return getText();
	}
	@Override
	protected Widget getTextBoxContainer() {
		return textBoxPanel;
	}
	protected void setTextBoxValue(final List<String> suggestions, final int suggestionIndex) {
		selectedStringList.add(suggestions.get(suggestionIndex));
		final InlineHTML label = new InlineHTML(suggestions.get(suggestionIndex));
		label.setStyleName("autocomplete-graphic-label");
		label.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				selectedStringList.remove(suggestions.get(suggestionIndex));
				label.removeFromParent();
			}});
		DOM.setStyleAttribute(label.getElement(), "display", "inline-block");
		textBoxPanel.insert(label, textBoxPanel.getWidgetCount() - 1);
	}
}